#include "Camera.h"
#include <cmath>
#include <vector>
#include "Vector2.h"

using namespace std;

Camera::Camera(const Player& p, Map& m): player(&p), map(&m) 
{
	update();
}

void Camera::update()
{
	
	GLfloat centerX = player->posX + (player->width / 2.0f);
	GLfloat centerY = player->posY + (player->height / 2.0f);
	int viewWidth = map->getViewWidth();
	int viewHeight = map->getViewHeight();
	
	// viewWidth / 2 seems to be okay (no truncation, don't need to cast to float)
	left = centerX - viewWidth/2;
	right = centerX + viewWidth/2;
	bottom = centerY - viewHeight/2;
	top = centerY + viewHeight/2;
	
	// Tell the map how much it needs to draw
	map->setMaxBounds(right, top);
	
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	
	glOrtho( left, right, bottom, top, -1, 1);
	
	
	
	
	
	Vector2 topLeft(ceil(abs(left)), ceil(abs(top)));
	Vector2 topRight(ceil(abs(right)), ceil(abs(top)));
	Vector2 bottomRight(ceil(abs(right)), ceil(abs(bottom)));
	Vector2 bottomLeft(ceil(abs(left)), ceil(abs(bottom)));
	
	bool tl,tr,br,bl;
	tl = map->outOfBounds( topLeft.x, topLeft.y );
	tr = map->outOfBounds( topRight.x, topRight.y );
	br = map->outOfBounds( bottomRight.x, bottomRight.y );
	bl = map->outOfBounds( bottomLeft.x, bottomLeft.y );
	
//	// vector containing view corners inside the map
//	vector<Vector2*> inside;
//	if (!tl)
//		inside.push_back(topLeft);
//	if (!tr)
//		inside.push_back(&tr);
//	if (!br)
//		inside.push_back(&br);
//	if (!bl)
//		inside.push_back(&bl);
//	
//	// If two or more corners are out of bounds, and at least one is in bounds
//	if ( !(inside.empty()) && inside.size() < 4)
//	{
//		
//		if (inside.size() < 2)	// one el
//		{
//			bool* c = inside[0];
//			if (c == &tl)
//			{
//			}
//			if (c == &tr)
//			{
//			}
//			if (c == &br)
//			{
//			}
//			if (c == &bl)
//			{
//			}
//		}
//		else					// two el
//		{
//			
//		}
//	}
	
	
	
	// If at least one (actually two) is out of bounds and at least one is in bounds.
	if ( (tl || tr || br || bl) && !(tl && tr && br && bl) )
	{
		
	}
}